package com.tensquare.friend.dao;

import com.tensquare.friend.pojo.Friend;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface FriendDao extends JpaRepository<Friend, String>, JpaSpecificationExecutor<Friend> {


    /**
     * 删除好友
     *
     * @param userid
     * @param friendid
     */
    @Modifying
    @Query(value = "DELETE FROM tb_friend WHERE userid = ?1 AND friendid = ?2", nativeQuery = true)
    public void deleteFriend(String userid, String friendid);

    /**
     * 根据用户ID与被关注用户ID查询记录个数
     *
     * @param userid 用户ID
     * @param friend 被关注用户ID
     * @return 个数
     */
    @Query(value = "SELECT COUNT(1) FROM tb_friend f WHERE userid = ?1 AND friendid = ?2", nativeQuery = true)
    public Integer selectCout(String userid, String friend);


    /**
     * 更改喜欢的状态
     *
     * @param userid   用户ID
     * @param friendid 被关注用户ID
     * @param islike   喜欢状态
     */
    @Modifying
    @Query(value = "UPDATE tb_friend SET islike = ?3 WHERE userid = ?1 AND friendid = ?2", nativeQuery = true)
    public void updateLike(String userid, String friendid, String islike);

}
